﻿using System;
using System.Runtime.InteropServices;

namespace FeiYang
{
    class 窗口_
    {
        // 导入 Windows API 函数
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        public const int SWP_NOSIZE = 0x0001;
        public const int SWP_NOMOVE = 0x0002;
        public const int SWP_NOZORDER = 0x0004;
        public const int GWL_STYLE = -16;
        public const long WS_MINIMIZE = 0x2000000L;

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [Flags]
        public enum ShowWindowCommands : int
        {
            SW_HIDE = 0,
            SW_SHOWNORMAL = 1,
            SW_NORMAL = 1,
            SW_SHOWMINIMIZED = 2,
            SW_SHOWMAXIMIZED = 3,
            SW_MAXIMIZE = 3,
            SW_SHOWNOACTIVATE = 4,
            SW_SHOW = 5,
            SW_MINIMIZE = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA = 8,
            SW_RESTORE = 9,
            SW_SHOWDEFAULT = 10,
            SW_FORCEMINIMIZE = 11,
            SW_MAX = 11
        }

        [DllImport("user32.dll")]
        public static extern ShowWindowCommands ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseWindow(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);

        public static bool 刷新窗口(IntPtr hWnd)
        {
            return InvalidateRect(hWnd, IntPtr.Zero, true);
        }
        public static bool 窗口是最小化的(IntPtr hWnd)
        {
            long style = 窗口_.GetWindowLong(hWnd, 窗口_.GWL_STYLE);
            return (style & 窗口_.WS_MINIMIZE) == 窗口_.WS_MINIMIZE;
        }

        public static void 最小化窗口(IntPtr hWnd)
        {
            窗口_.ShowWindow(hWnd, 窗口_.ShowWindowCommands.SW_MINIMIZE);
        }

        public static void 置顶窗口(IntPtr hWnd)
        {
            窗口_.SetWindowPos(hWnd, -1, 0, 0, 0, 0, 窗口_.SWP_NOSIZE);
        }

        public static void 恢复窗口(IntPtr hWnd)
        {
            窗口_.ShowWindow(hWnd, 窗口_.ShowWindowCommands.SW_RESTORE);
        }

        public static bool 关闭窗口(IntPtr hWnd)
        {
            return 窗口_.CloseWindow(hWnd);
        }

    }
}
